package com.penjin.android.message.contact;

import com.easemob.chat.EMChatManager;
import com.penjin.android.R;
import com.penjin.android.domain.PenjinEaseUser;
import com.penjin.android.domain.PenjinUser;
import com.penjin.android.message.EMChatHelper;
import com.penjin.android.message.chat.EMChatActivity;
import com.penjin.android.message.domain.EaseUser;
import com.penjin.android.message.utils.EaseConstant;
import com.penjin.android.message.vedio.VoiceCallActivity;
import com.penjin.android.service.UserService;
import com.penjin.android.utils.ToastHelper;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class EMContactDetailActivity extends Activity implements View.OnClickListener {

    TextView usernameTextView;
    String username;
    int type;
    PenjinEaseUser user;

    View emailWrapper;
    TextView email;
    View addressWrapper;
    TextView address;

    View staffInfoWrapper;
    View phoneWrapper;
    TextView phone;
    TextView realName;
    TextView department;
    TextView position;

    PenjinUser currentUser;//当前登录用户个人信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_contact_detail);
        initData(savedInstanceState);
        initView();
    }

    private void initData(Bundle savedInstance) {
        Intent intent = getIntent();
        if (intent != null) {
            this.username = intent.getStringExtra(EaseConstant.EXTRA_USER_ID);
            this.type = intent.getIntExtra(EaseConstant.EXTRA_CHAT_TYPE, 1);
        }
        if (savedInstance != null) {
            String tmp1 = savedInstance.getString(EaseConstant.EXTRA_USER_ID);
            int tmp2 = savedInstance.getInt(EaseConstant.EXTRA_USER_ID);
            if (tmp1 != null) {
                this.username = tmp1;
            }
            this.type = tmp2;
        }
        currentUser = UserService.getInstance(this).getCurrentUser();
        EaseUser easeUser = EMChatHelper.getInstance().getUserInfo(this.username);
        if (easeUser == null) {
            ToastHelper.showMessage("好友关系已经解除", getApplicationContext());
            finish();
        } else {
            this.user = (PenjinEaseUser) easeUser;
        }

    }

    private void initView() {
        if (user != null) {
            usernameTextView = (TextView) findViewById(R.id.background_name);
            usernameTextView.setText(this.user.getNick());
            emailWrapper = findViewById(R.id.emailWrapper);
            email = (TextView) findViewById(R.id.email);
            addressWrapper = findViewById(R.id.addressWrapper);
            address = (TextView) findViewById(R.id.address);
            emailWrapper.setOnClickListener(this);
            addressWrapper.setOnClickListener(this);
            email.setText(user.getEmail());
            address.setText(user.getAddress());


            if (currentUser.isInCompany()) {
                if (currentUser.getCompanyId().equals(user.getCompanyId())) {
                    staffInfoWrapper = findViewById(R.id.staffInfoWrapper);
                    staffInfoWrapper.setVisibility(View.VISIBLE);
                    phoneWrapper = findViewById(R.id.phoneWrapper);
                    phone = (TextView) findViewById(R.id.phone);
                    department = (TextView) findViewById(R.id.department);
                    position = (TextView) findViewById(R.id.position);
                    realName = (TextView) findViewById(R.id.realName);
                    phone.setText(user.getPhone());
                    department.setText(user.getDepartment());
                    position.setText(user.getPosition());
                    realName.setText(user.getRealname());
                    phoneWrapper.setOnClickListener(this);
                }
            }
        }
    }

    public void sendMsg(View view) {
        Intent intent = new Intent(this, EMChatActivity.class);
        Bundle extras = new Bundle();
        extras.putString(EaseConstant.EXTRA_USER_ID, this.username);
        extras.putInt(EaseConstant.EXTRA_CHAT_TYPE, this.type);
        intent.putExtras(extras);
        startActivity(intent);
        finish();
    }

    public void sendVoice(View view) {
        if (!EMChatManager.getInstance().isConnected()) {
            Toast.makeText(this, R.string.not_connect_to_server, Toast.LENGTH_SHORT)
                    .show();
        } else {
            startActivity(new Intent(this, VoiceCallActivity.class)
                    .putExtra("username", this.username).putExtra(
                            "isComingCall", false));
        }
    }

    /**
     * 拨打真实电话
     *
     * @param view
     */
    public void sendDing(View view) {

    }

    public void back(View view) {
        finish();
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(EaseConstant.EXTRA_USER_ID, this.username);
        outState.putInt(EaseConstant.EXTRA_CHAT_TYPE, this.type);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(View v) {

    }
}
